package com.lhy.service;

import cn.hutool.core.util.IdUtil;
import com.lhy.common.vo.Result;
import com.lhy.pojo.Devices;
import com.lhy.pojo.Miner;
import com.lhy.pojo.User;
import com.mongodb.client.result.DeleteResult;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private MongoTemplate mongoTemplate;

    //添加用户信息到mongodb(超级管理员权限，只有超级管理员才能使用此功能）
    public void addUser(User user){
        //雪花算法生成用户编号
        user.setUserId(IdUtil.getSnowflake(1,1).nextIdStr());
        //更新时间
        user.setUpdateTime(new Date());
        //添加矿工信息到mongodb
        mongoTemplate.insert(user,"user");
    }

    public Result login(User user, @RequestParam("captcha") String captcha, HttpServletRequest request) {
        //验证
        if (!CaptchaUtil.ver(captcha, request)) {
            return Result.fail("验证码错误");
        }

        Query query = new Query(Criteria.where("username").is(user.getUsername()).and("password").is(user.getPassword()).and("authority").is(user.getAuthority()));
        Boolean exists = mongoTemplate.exists(query, User.class, "user");
        Integer authority=user.getAuthority();


        if (exists == false) {
            //登陆失败
            return Result.fail("用户名、密码或权限错误");
        }

        if (authority == 0) {
            //主任工程师登陆成功
            return Result.managerSuccess();
        }

        //监察员登陆成功
        if(authority == 1) {
            return Result.inspectorSuccess();
        }

        //普通员工登陆成功
        return Result.userSuccess();

    }

    //查询所有用户
    public Map<String,Object> selectUserList(){
        List<User> list = mongoTemplate.findAll(User.class,"user");
        Map<String,Object> userResult =new HashMap<>();
        if(list==null || list.isEmpty()){
            userResult.put("code","400");
            userResult.put("message","没有相关信息");
        }else{
            userResult.put("code","0");
            userResult.put("count",list.size());
            userResult.put("data",list);
        }
        return userResult;
    }

    //根据账号删除
    public boolean deleteUserById(String id){
        Query query = new Query(Criteria.where("userId").is(id));
        DeleteResult result = mongoTemplate.remove(query, User.class, "user");
        return result.getDeletedCount() >0 ? true : false;
    }

    //根据工号修改矿工信息
    public void updateUserInfo(User user){
        Query query = new Query(Criteria.where("userId").is(user.getUserId()));
        Boolean exists = mongoTemplate.exists(query, User.class, "user");
        if(exists==false) return;
        Update update=new Update();
        user.setUpdateTime(new Date());

        if(user.getUsername()!="") update.set("username",user.getUsername());

        update.set("authority",user.getAuthority());

        if(user.getPhone()!="") update.set("phone",user.getPhone());

        if(user.getPassword()!="") update.set("password",user.getPassword());

        if(user.getEmail()!="") update.set("email",user.getEmail());

        mongoTemplate.upsert(query,update,User.class,"user");
    }

    //通过用户账号id查询
    public User selectUserById(String userId){
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query,User.class,"user");
    }


}
